﻿var username_check = false;
$(function () {
    $("a").focus(function () { this.blur() });
    $("input[type='button']").focus(function () { this.blur() });

    inputChangeColor("username");
    inputChangeColor("password");

    changeFontColor('username', '手机/昵称', '');
    changeFontColor('password', '密码', '');

    $('#username').focus(function () {
        changeFontColor('username', '手机/昵称', '');
    });
    $('#username').bind('blur', function () {
        check_username();
    });


    $('#password').bind('blur', function () {
        check_pwd();
    });

//    $('#authcode').blur(function () {
//        check_auth();
//    });

});
function check_username() {
    var str = '';
    username_check = true;
    var username = $('#username').val();
    if (!username || username == '手机/昵称') {
        username_check = false;
        str = '<div class="login_prompt" id="tips_username"><dl>帐号不能为空。</dl></div>';
    } else if (username.length > 31) {
        str = '<div class="login_prompt" id="tips_username"><dl>帐号最多31位。</dl></div>';
        username_check = false;
    }
    $('#tips_username').replaceWith(str);
    if (!$('#tips_username').text()) {
        return true;
    }
    return username_check;
}

window.checkUserName = check_username;

function check_pwd() {
    var flag = false;
    var ret = checkPwd($('#password'));
    var str = '';
    switch (ret) {
        case 0: str = '密码不能为空。'; break;
        case -1: str = '密码长度应在6～16个字符。'; break;
        case 1: str = ''; flag = true; break;
    }
    $('#tips_password').replaceWith('<div class="login_prompt" id="tips_password"><dl>' + str + '</dl></div>');
    return flag;
}

function submitUserInfo() {
    if (!check_username()) {
        return false;
    };
    if (!check_pwd()) {
        return false;
    };

//    if (!check_auth()) {
//        return false;
//    }
    return true;
}

function login() {
    if (submitUserInfo()) {
        var username = $('#username').val();
        var password = $('#password').val();

        $.ajax2({
            type: "post",
            contentType: "application/json",
            url: "../WebServices/LoginAndRegist.asmx/Login",
            dataType: "json",
            data: "{ userName: '" + username + "',password:'" + password + "',isAutoLogin:'" + isChecked + "' }",
            success: function (data) {
                if (data.d.Flag) {
                    window.location.href = "/";
                } else {
                    $('#tips_password').replaceWith('<div class="login_prompt" id="tips_password"><dl>' + data.d.Message + '</dl></div>');
                }
            },
            error: function (error) {
                $('#tips_password').replaceWith('<div class="login_prompt" id="tips_password"><dl> 服务器发生错误，请稍后再试</dl></div>');
            }

        });

    }
}
var isChecked = true;
function checkKeep(obj) {
    var cb = $(obj).attr("checked");
    if (cb) {
        $(obj).attr("checked", false);
        isChecked = false;
    } else {
        $(obj).attr("checked", true);
        isChecked = true;
    }
}

